package ui.activity.home.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import base.BaseBiz;
import coom.RetrofitHelp;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.home.biz.CreditCardsBiz;
import ui.activity.home.contract.CreditCardsContract;
import ui.model.CreditCardsBean;
import ui.model.ShareParamBean;

/**
 * Created by chenjian on 19/2/26
 */

public class CreditCardsPresenter implements CreditCardsContract.Presenter {

    CreditCardsContract.View view;
    CreditCardsBiz creditCardsBiz;
    List<CreditCardsBean> list = new ArrayList<>();


    @Inject
    public CreditCardsPresenter(CreditCardsContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        creditCardsBiz = (CreditCardsBiz) biz;
    }


    @Override
    public void getList(boolean isRefresh, String id) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        String url = RetrofitHelp.BASE_URL + "api/revision/list/" + id;
        creditCardsBiz.getCardList(url, new BaseBiz.Callback<List<CreditCardsBean>>() {
            @Override
            public void onSuccess(List<CreditCardsBean> info) {
                list.addAll(info);
                view.upDateUI(list);
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
            }

            @Override
            public void onFailure(List<CreditCardsBean> info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });
    }

    @Override
    public void toShare() {
        view.setViewEnable(false);
        view.loading(true);
        ShareParamBean shareParamBean=new ShareParamBean();
        shareParamBean.setFlat("");
        shareParamBean.setShareTo("");
        shareParamBean.setType("1");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(shareParamBean));
        creditCardsBiz.getShareList(requestBody, new BaseBiz.Callback<String>() {
            @Override
            public void onSuccess(String info) {
                view.upDataShare(info);
                view.setViewEnable(true);
                view.loading(false);
            }

            @Override
            public void onFailure(String info) {
                view.loading(false);
                view.setViewEnable(true);
            }
        });
    }

}
